import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ConfirmPurchasePage extends JPanel {
    public ConfirmPurchasePage(CardLayout cardLayout, JPanel cardPanel) {
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);

        // 顶部标题
        JLabel titleLabel = new JLabel("购买宝贝", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0));
        add(titleLabel, BorderLayout.NORTH);

        // 用户信息
        JPanel contentPanel = createContentPanel();
        JScrollPane scrollPane = new JScrollPane(contentPanel);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(scrollPane, BorderLayout.CENTER);

        // 底部确认和返回
        JPanel bottomPanel = createBottomPanel(cardLayout, cardPanel);
        add(bottomPanel, BorderLayout.SOUTH);
    }

    private JPanel createContentPanel() {
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));

        // 用户信息
        JPanel userPanel = new JPanel();
        userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.Y_AXIS));
        userPanel.setBackground(Color.WHITE);
        
        userPanel.add(createInfoLabel("用户名：张三", Font.BOLD));
        userPanel.add(createInfoLabel("电话：135****7968", Font.PLAIN));
        userPanel.add(createInfoLabel("详细地址：辽宁省沈阳市沈北新区", Font.PLAIN));
        userPanel.add(createInfoLabel("沈阳航空航天大学", Font.PLAIN));
        userPanel.add(Box.createVerticalStrut(15));
        
        // 价格信息
        JPanel pricePanel = new JPanel(new BorderLayout());
        pricePanel.setBackground(Color.WHITE);
        
        JLabel priceLabel = new JLabel("¥899.00");
        priceLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        priceLabel.setForeground(new Color(255, 80, 80));
        pricePanel.add(priceLabel, BorderLayout.WEST);
        
        userPanel.add(pricePanel);
        userPanel.add(Box.createVerticalStrut(15));
        
        // 费用明细
        userPanel.add(createFeeItem("配送费", "¥0.00"));
        userPanel.add(createFeeItem("红包代金券", "¥0.00"));
        userPanel.add(Box.createVerticalStrut(20));
        
        // 购买须知
        JTextArea noticeArea = new JTextArea(
            "购买须知\n" +
            "1、自由市场商品，发货后不支持无理由退货\n" +
            "2、收到包裹时，请仔细验收并拍摄完整开箱视频（商品有问题时，这是强有力的凭证）\n" +
            "3、不要轻信操作【确认收货】后才能给您售后、返现、部分退款等承诺"
        );
        noticeArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        noticeArea.setEditable(false);
        noticeArea.setLineWrap(true);
        noticeArea.setWrapStyleWord(true);
        noticeArea.setBackground(new Color(250, 250, 250));
        noticeArea.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(230, 230, 230)),
            BorderFactory.createEmptyBorder(10, 10, 10, 10)
        ));
        
        userPanel.add(noticeArea);
        contentPanel.add(userPanel);

        return contentPanel;
    }

    private JLabel createInfoLabel(String text, int style) {
        JLabel label = new JLabel(text);
        label.setFont(new Font("微软雅黑", style, 14));
        label.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));
        return label;
    }

    private JPanel createFeeItem(String name, String value) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);
        
        JLabel nameLabel = new JLabel(name);
        nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        
        JLabel valueLabel = new JLabel(value);
        valueLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        valueLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        
        panel.add(nameLabel, BorderLayout.WEST);
        panel.add(valueLabel, BorderLayout.EAST);
        panel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
        
        return panel;
    }

    private JPanel createBottomPanel(CardLayout cardLayout, JPanel cardPanel) {
        JPanel bottomPanel = new JPanel(new BorderLayout());
        bottomPanel.setBackground(new Color(250, 250, 250));
        bottomPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(230, 230, 230)));

        // 确认购买按钮
        JButton confirmButton = new JButton("应付款 ￥899.00    确认购买");
        confirmButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        confirmButton.setBackground(new Color(255, 80, 80));
        confirmButton.setForeground(Color.WHITE);
        confirmButton.setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0));
        bottomPanel.add(confirmButton, BorderLayout.CENTER);

        // 关闭按钮
        JButton closeButton = new JButton("☒");
        closeButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        closeButton.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
        closeButton.addActionListener(e -> cardLayout.show(cardPanel, "purchaseInfo"));
        bottomPanel.add(closeButton, BorderLayout.EAST);

        return bottomPanel;
    }
}